package dialogs;

import frame.MainFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import sqlmanager.Authentification;

public class OpenDialog extends JDialog {
	private final String OKAC = "ok";
	private final String CancelAC = "cancel";

	private JPanel pane = null;
	private JTabbedPane typeTabbedPane = null;
	private JPanel odbcPanel = null;
	private JPanel jdbcPanel = null;
	private ConnectionPanel odbcConnectionPanel = null;
	private ConnectionPanel jdbcConnectionPanel = null;
	private JPanel dbmsPanel = null;
	private JRadioButton mysqlRadioButton = null;
	private JRadioButton sqlserverRadioButton = null;
	private JPanel buttonsPanel = null;
	private JButton okButton = null;
	private JButton cancelButton = null;

	private MainFrame parentFrame = null;
	private ButtonsHandler buttonsHandler = null;
	private Authentification authentification = null;

	/**
	 * This is the default constructor
	 */
	public OpenDialog(MainFrame parentFrame) {
		super();
		initialize();
		// this.setAlwaysOnTop( true );
		this.getAuthentification();
		this.parentFrame = parentFrame;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(465, 285);
		this.setTitle("������"); // Generated
		this.setContentPane(getPane()); // Generated
	}

	/**
	 * This method initializes pane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getPane() {
		if (pane == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.insets = new java.awt.Insets(3, 0, 3, 0); // Generated
			gridBagConstraints1.gridy = 1; // Generated
			gridBagConstraints1.ipadx = 147; // Generated
			gridBagConstraints1.gridx = 0; // Generated
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; // Generated
			gridBagConstraints.gridx = 0; // Generated
			gridBagConstraints.gridy = 0; // Generated
			gridBagConstraints.ipadx = 252; // Generated
			gridBagConstraints.weightx = 10.0; // Generated
			gridBagConstraints.weighty = 10.0; // Generated
			gridBagConstraints.insets = new java.awt.Insets(6, 8, 2, 8); // Generated
			pane = new JPanel();
			pane.setLayout(new GridBagLayout()); // Generated
			pane.add(getTypeTabbedPane(), gridBagConstraints); // Generated
			pane.add(getButtonsPanel(), gridBagConstraints1); // Generated
		}
		return pane;
	}

	/**
	 * This method initializes typeTabbedPane
	 * 
	 * @return javax.swing.JTabbedPane
	 */
	private JTabbedPane getTypeTabbedPane() {
		if (typeTabbedPane == null) {
			typeTabbedPane = new JTabbedPane();
			typeTabbedPane.addTab("ODBC ����", null, getOdbcPanel(), null); // Generated
			typeTabbedPane.addTab("JDBC ����", null, getJdbcPanel(), null); // Generated
		}
		return typeTabbedPane;
	}

	/**
	 * This method initializes odbcPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getOdbcPanel() {
		if (odbcPanel == null) {
			odbcPanel = new JPanel();
			odbcPanel.setLayout(new BorderLayout()); // Generated
			odbcPanel.add(getOdbcConnectionPanel(), java.awt.BorderLayout.NORTH); // Generated
			odbcPanel.add(getOdbcConnectionPanel(), java.awt.BorderLayout.CENTER); // Generated
		}
		return odbcPanel;
	}

	/**
	 * This method initializes jdbcPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJdbcPanel() {
		if (jdbcPanel == null) {
			jdbcPanel = new JPanel();
			jdbcPanel.setLayout(new BorderLayout()); // Generated
			jdbcPanel.add(getDbmsPanel(), java.awt.BorderLayout.NORTH); // Generated
			jdbcPanel.add(getJdbcConnectionPanel(), java.awt.BorderLayout.CENTER); // Generated
			jdbcPanel.add(this.getJdbcConnectionPanel(), BorderLayout.CENTER);
		}
		return jdbcPanel;
	}

	private ConnectionPanel getOdbcConnectionPanel() {
		if (odbcConnectionPanel == null) {
			odbcConnectionPanel = new ConnectionPanel();
			odbcConnectionPanel.disablePort();
		}
		return odbcConnectionPanel;
	}

	private ConnectionPanel getJdbcConnectionPanel() {
		if (jdbcConnectionPanel == null) {
			jdbcConnectionPanel = new ConnectionPanel();
		}
		return jdbcConnectionPanel;
	}

	/**
	 * This method initializes dbmsPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getDbmsPanel() {
		if (dbmsPanel == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(1); // Generated
			gridLayout.setColumns(2); // Generated
			dbmsPanel = new JPanel();
			dbmsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "DBMS", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
					javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); // Generated
			dbmsPanel.setLayout(gridLayout); // Generated
			dbmsPanel.add(getMysqlRadioButton(), null); // Generated
			dbmsPanel.add(getSqlserverRadioButton(), null); // Generated

			ButtonGroup group = new ButtonGroup();
			group.add(this.mysqlRadioButton);
			group.add(this.sqlserverRadioButton);
		}
		return dbmsPanel;
	}

	/**
	 * This method initializes mysqlRadioButton
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getMysqlRadioButton() {
		if (mysqlRadioButton == null) {
			mysqlRadioButton = new JRadioButton();
			mysqlRadioButton.setText("MySQL"); // Generated
			mysqlRadioButton.setSelected(true); // Generated
		}
		return mysqlRadioButton;
	}

	/**
	 * This method initializes sqlserverRadioButton
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getSqlserverRadioButton() {
		if (sqlserverRadioButton == null) {
			sqlserverRadioButton = new JRadioButton();
			sqlserverRadioButton.setText("SQL Server"); // Generated
			sqlserverRadioButton.setEnabled(false); // Generated
		}
		return sqlserverRadioButton;
	}

	/**
	 * This method initializes buttonsPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getButtonsPanel() {
		if (buttonsPanel == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setVgap(5); // Generated
			flowLayout.setHgap(50); // Generated
			buttonsPanel = new JPanel();
			buttonsPanel.setLayout(flowLayout); // Generated
			buttonsPanel.add(getOkButton(), null); // Generated
			buttonsPanel.add(getCancelButton(), null); // Generated
		}
		return buttonsPanel;
	}

	/**
	 * This method initializes okButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK"); // Generated
			okButton.setActionCommand(this.OKAC);
			okButton.setSelected(true); // Generated
			okButton.setMnemonic(java.awt.event.KeyEvent.VK_O); // Generated
			okButton.setPreferredSize(new java.awt.Dimension(75, 28)); // Generated
			okButton.addActionListener(this.getButtonsHandler());
			okButton.setDefaultCapable(true);
		}
		return okButton;
	}

	/**
	 * This method initializes cancleButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel"); // Generated
			cancelButton.setActionCommand(this.CancelAC);
			cancelButton.setMnemonic(java.awt.event.KeyEvent.VK_C); // Generated
			cancelButton.setPreferredSize(new java.awt.Dimension(75, 28)); // Generated
			cancelButton.addActionListener(this.getButtonsHandler());
		}
		return cancelButton;
	}

	public Authentification getAuthentification() {
		if (null == authentification)
			authentification = new Authentification();
		return authentification;
	}

	private void updateAuthentification() {
		ConnectionPanel selectPane = null;
		if (odbcPanel == typeTabbedPane.getSelectedComponent()) {
			selectPane = odbcConnectionPanel;
			authentification.type = Authentification.ODBC;
		} else if (jdbcPanel == typeTabbedPane.getSelectedComponent()) {
			selectPane = jdbcConnectionPanel;
			authentification.type = Authentification.JDBC;
		}

		authentification.source = selectPane.getSourceTextField().getText();
		authentification.user = selectPane.getUserTextField().getText();
		authentification.password = new String(selectPane.getPasswordField().getPassword());
		authentification.port = Integer.parseInt(selectPane.getPortTextField().getText());

	}

	private ButtonsHandler getButtonsHandler() {
		if (null == buttonsHandler)
			buttonsHandler = new ButtonsHandler();
		return buttonsHandler;
	}

	class ButtonsHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String ac = e.getActionCommand();
			if (ac.equals(OKAC)) {
				OpenDialog.this.updateAuthentification();
				System.out.println(authentification);
				OpenDialog.this.setVisible(false);
				parentFrame.connectTo(authentification);
			} else if (ac.equals(CancelAC))
				OpenDialog.this.setVisible(false);
		}
	}

} // @jve:decl-index=0:visual-constraint="10,10"
